<?php
class Model_Log
{
    public static function write($content)
    {
        $year  = date('Y',Model_Common::getNowTime());
        $month = date('Ym',Model_Common::getNowTime());
        $day   = date('Ymd',Model_Common::getNowTime());
        $forder = PROJECT_PATH . "Log/{$year}/{$month}";
        if(!file_exists($forder)){
            mkdir($forder, 0777, TRUE);
        }
        $fileName = PROJECT_PATH . "Log/{$year}/{$month}/error{$day}.log";
        
        $title = Model_Common::getFormatTime() . " ";
        if(Model_User::isSignIn()){
        	$userid   = Model_User::getUserID();
        	$username = Model_User::getUserName();
        	$levelid  = Model_User::getLevelID();
        	$title   .= "[userid:{$userid}][username:{$username}][levelid:{$levelid}]";
        }
        $URL = Eren_Request_URL::getURL();
        $IP = Eren_Request_URL::getIP();
        $title .= "[url:{$URL}][fromip:{$IP}][log:{$content}]";
        Eren_File::append($fileName, $title . "\r\n");
    }
    
    public static function start()
    {
        Eren_Timer::start();
        $content = "\r\n\r\n[Visit URL]" . Eren_Request_URL::getURL();
        $content .= "\r\n[FROM IP]" . Eren_Request_URL::getIP();
        $content .= "\r\n[Visite Time]" . Model_Common::getFormatTime(Model_Common::getNowTime()); 
        self::write($content);
    }
    
    public static function end()
    {
        $useTime = Eren_Timer::end();
        $content = "\r\n[Used Time]{$useTime}";
        self::write($content);
    }
}